
package com.tools.cleanmaster.ui.view.header;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.tools.cleanmaster.R;

@SuppressWarnings("static-access")
public class HeadHelper {
    public static Header setupMainHeader(Activity activity, int titlebarId, int titleStringRes,
                                         OnBackStackListener listener) {
        return Header.create(activity, titlebarId).setTitle(titleStringRes).setLogo(
                R.drawable.titlebar_logo_back, listener);
    }

    public static Header setupMainHeader(Activity activity, int titlebarId, String titleStringRes,
                                         OnBackStackListener listener) {
        return Header.create(activity, titlebarId).setTitle(titleStringRes).setLogo(
                R.drawable.titlebar_logo_back, listener);
    }

    public static Header setupMainHeader(Activity activity, int titlebarId, int titleStringRes) {
        return Header.create(activity, titlebarId).setTitle(titleStringRes);
    }

    public static Header setupMainHeader(Activity activity, int titlebarId, int logoResId,
                                         String title, OnBackStackListener listener) {
        return Header.create(activity, titlebarId).setTitle(title).setLogo(logoResId, listener);
    }

    public static View getListSeparatorView(Context cxt) {
        View sepView = new View(cxt);
        sepView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2)); // 2
        // pixels
        // height
        sepView.setBackgroundResource(R.drawable.list_items_devider);
        return sepView;
    }

    /**
     * If you didn't need srcView's onClick event,@see
     * #dispatchOnclickEvent(final View srcView, final View dstView)
     *
     * @param srcView:The            view which get the onClick event
     * @param dstView:The            view which will accept the onClick event
     * @param srcOnClickListener:The srcView 's setting Listener will be
     *                               override, so you should recall it.
     * @param srcOnClickFirst
     */
    public static void dispatchOnclickEvent(final View srcView, final View dstView,
                                            final OnClickListener srcOnClickListener, final boolean srcOnClickFirst) {
        if (srcView == null) {
            return;
        }

        srcView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (srcOnClickFirst) {
                    if (srcOnClickListener != null) {
                        srcOnClickListener.onClick(srcView);
                    }

                    if (dstView != null) {
                        dstView.performClick();
                    }
                } else {
                    if (dstView != null) {
                        dstView.performClick();
                    }

                    if (srcOnClickListener != null) {
                        srcOnClickListener.onClick(srcView);
                    }
                }
            }
        });
    }

    /**
     * @param srcView
     * @param dstView
     *
     * @see #dispatchOnclickEvent(View, View, OnClickListener, boolean)
     */
    public static void dispatchOnclickEvent(final View srcView, final View dstView) {
        dispatchOnclickEvent(srcView, dstView, null, true);
    }

    private static long mLastTime = 0;

    /**
     * @param invalidTime The invalid time between double click
     *
     * @return
     */
    public static boolean isButtonClickTooFast(int invalidTime) {
        if (invalidTime < 0) {
            invalidTime = 0;
        }
        long currentTime = System.currentTimeMillis();
        if (currentTime - mLastTime > invalidTime) {
            mLastTime = currentTime;
            return false;
        }
        return true;
    }
}
